package com.android.nurse;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.Button;

public class TopScreen extends CommonActivity {

	private Button btnNJB;
	private Button btnRanking;
	private Button btnConsultation;
	public static int updateTimes = 0;
	private MyHandler myHandler = new MyHandler();
	private int width;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Display display = ((WindowManager) this
				.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
		width = display.getWidth();
		Log.d("on", "On create");
		if (width > 500) {
			setContentView(R.layout.topscreen_big);
		} else {
			setContentView(R.layout.topscreen);
		}

		btnConsultation = (Button) findViewById(R.id.btnConsultation);
		btnNJB = (Button) findViewById(R.id.btnNJB);
		btnRanking = (Button) findViewById(R.id.btnRanking);
		disableUI();
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		// super.onBackPressed();
		// this.updateTimes = 0;
		return;
	}
	public void disableUI() {
		Log.d("on","Disable UI");
		btnConsultation.setClickable(false);
		btnRanking.setClickable(false);
		btnNJB.setClickable(false);
	}

	public void postResult() {
		Log.d("start", "thread start");
		LocalRanking lx = new LocalRanking(null, getApplicationContext());
		lx.tryToResend(getPostURL(), getShowRanking());
		lx.getListLocalRankToShow(getShowRanking(), getGetURL());
		updateTimes++;
		//myHandler.sendEmptyMessage(0);
	}

	class MyHandler extends Handler {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			Log.d("start", "my handler started");
			enableUI();
		}

	}

	/*
	 * private Thread myThread = new Thread(new Runnable() {
	 * 
	 * @Override public void run() {
	 */
	public void enableUI() {

		// TODO Auto-generated method stub
		Log.d("on","Enable UI");
		btnRanking.setClickable(true);
		btnNJB.setClickable(true);
		btnConsultation.setClickable(true);
		btnRanking.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				switch (event.getAction() & MotionEvent.ACTION_MASK) {

				case MotionEvent.ACTION_DOWN:
					if (width > 500) {
						btnRanking
								.setBackgroundResource(R.drawable.btn_ranking_top_2_big);
					} else {
						btnRanking
								.setBackgroundResource(R.drawable.btn_ranking_top_2);
					}
					break;
				case MotionEvent.ACTION_UP:
					if (width > 500) {
						btnRanking
								.setBackgroundResource(R.drawable.btn_ranking_top_big);
					} else {
						btnRanking
								.setBackgroundResource(R.drawable.btn_ranking_top);
					}
					Intent i = new Intent(getApplicationContext(),
							RankingActivity.class);
					SharedPreferences settings = getSharedPreferences(
							QuestionsScreen.PREFS_NAME, 0);
					SharedPreferences.Editor edit = settings.edit();
					edit.remove("VIEWED");
					edit.commit();
					i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					customeActivity(i);
					break;
				}
				return true;
			}
		});

		btnNJB.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction() & MotionEvent.ACTION_MASK) {

				case MotionEvent.ACTION_DOWN:
					if (width > 500) {
						btnNJB
								.setBackgroundResource(R.drawable.btn_njb_top_big_2);
					} else {
						btnNJB.setBackgroundResource(R.drawable.btn_njb_top_2);
					}
					break;
				case MotionEvent.ACTION_UP:
					if (width > 500) {
						btnNJB
								.setBackgroundResource(R.drawable.btn_njb_top_big);
					} else {
						btnNJB.setBackgroundResource(R.drawable.btn_njb_top);
					}
					Intent i = new Intent(getApplicationContext(),
							NjbScreen.class);
					customeActivity(i);
					break;
				}
				return true;
			}
		});

		btnConsultation.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				switch (event.getAction() & MotionEvent.ACTION_MASK) {

				case MotionEvent.ACTION_DOWN:
					if (width > 500) {
						btnConsultation
								.setBackgroundResource(R.drawable.btn_consultation_big_2);
					} else {
						btnConsultation
								.setBackgroundResource(R.drawable.btn_consultation_2);
					}
					break;
				case MotionEvent.ACTION_UP:
					if (width > 500) {
						btnConsultation
								.setBackgroundResource(R.drawable.btn_consultation_big);
					} else {
						btnConsultation
								.setBackgroundResource(R.drawable.btn_consultation);
					}
					Intent i = new Intent(getApplicationContext(),
							LogInScreen.class);
					customeActivity(i);
					break;
				}

				return true;
			}
		});
	}

	/* }); */

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		Log.d("on", "On restart");
		disableUI();
		updateTimes = 0;
	}

	class MyAsyncTask extends AsyncTask<Void, Void, Void> {

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			enableUI();
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			postResult();
			return null;
		}

	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		if (updateTimes == 0) {
			Log.d("on", "On start");
			new MyAsyncTask().execute(null, null, null);
			updateTimes++;
		}else{
			enableUI();
		}
	}
}
